<template>
  <div class="login-success flex">
    <c-header :active="type" @changeType="getType"></c-header>
    <div class="title">{{name}}</div>
    <div class="login-main main">
      <div class="dealer-list" v-if="type == 0">
        <file-list
          :file_types="file_types"
          :product_types="product_types"
          @getId="checkConent"
          v-show="show"
        ></file-list>
        <chanse-file :downloadFile="downloadFile" :name="name" v-show="!show"></chanse-file>
      </div>
      <div class="downLoad-list" v-else>
        <download-list :file_types="typesName" @getFileId="checkFile" v-show="show"></download-list>
        <chanse-file :downloadFile="fileList" :name="name" v-show="!show"></chanse-file>
      </div>
      <div v-show="!show" class="back">
        <p v-if="fileList.length == 0 && downloadFile.length == 0">
          暂无信息
          <a href="javascript:;" @click="back">返回</a>
        </p>
        <x-button mini @click.native="back" v-else>返回</x-button>
      </div>
    </div>
    <c-footer :type="type" @changeType="getType"></c-footer>
  </div>
</template>

<script>
import { agentCenter, agentCenterFileList, getDownload } from "@/api/index";
import { XButton } from "vux";
import FileList from "@/components/FileList/FileList";
import ChanseFile from "@/components/ChanseFile/ChanseFile";
import downloadList from "@/components/downloadList/downloadList";
import { mapGetters } from "vuex";
export default {
  components: {
    FileList,
    ChanseFile,
    downloadList,
    XButton
  },
  data() {
    return {
      show: true,
      name: this.$route.query.type == 0 ? "经销商中心" : "下载中心",
      file_types: [],
      product_types: [],
      downloadFile: [],
      typesName: [],
      fileList: []
    };
  },
  computed: {
    ...mapGetters(["type"])
  },
  created() {
    if (this.type == 0) {
      let token = localStorage.getItem("token");
      if (token) {
        this.initDealer();
      } else {
        this.$vux.toast.text("请先登录", "top");
        this.$router.push({
          path: "/dealer"
        });
      }
    } else if (this.type == 1) {
      this.initDownLoad();
    }
  },
  watch: {
    type(val, oldValue) {
      if (val == 0) {
        let token = localStorage.getItem("token");
        if (token) {
          this.initDealer();
        } else {
          this.$vux.toast.text("请先登录", "top");
          this.$router.push({
            path: "/dealer"
          });
        }
      } else if (val == 1) {
        this.initDownLoad();
      }
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (vm.type == 0) {
        document.title = "经销商中心";
        let token = localStorage.getItem("token");
        if (token) {
          next();
        } else {
          vm.$vux.toast.text("请先登录", "top");
          vm.$router.push({
            path: "/dealer"
          });
        }
      } else if (vm.type == 1) {
        document.title = "下载中心";
        next();
      }
    });
  },
  methods: {
    getType(type) {
      this.$store.dispatch("setType/getType", type);
      this.name = this.type == 0 ? "经销商中心" : "下载中心";
      this.show = true;
    },
    initDealer() {
      agentCenter().then(res => {
        this.file_types = res.data.file_types;
        this.product_types = res.data.product_types;
      });
    },
    initDownLoad() {
      getDownload().then(res => {
        this.typesName = res.data.types;
      });
    },
    back() {
      this.name = this.type == 0 ? "经销商中心" : "下载中心";
      this.show = !this.show;
    },
    checkConent(objPar) {
      let title = this.type == 0 ? "经销商中心" : "下载中心";
      this.name = `${title}-${objPar.name}-${objPar.productName}`;
      let obj = {
        type: objPar.difference,
        type_id: objPar.id
      };
      agentCenterFileList(obj).then(res => {
        res.data.rows.forEach((e, i) => {
          res.data.rows[i].checked = false;
        });
        this.downloadFile = res.data.rows;
        this.show = false;
      });
    },
    checkFile(objFile) {
      let title = this.type == 0 ? "经销商中心" : "下载中心";
      this.name = `${title}-${objFile.fileName}`;
      let obj = {
        type_id: objFile.id,
        page: ""
      };
      getDownload(obj).then(res => {
        res.data.rows.forEach((e, i) => {
          res.data.rows[i].checked = false;
        });
        this.fileList = res.data.rows;
        this.show = false;
      });
    }
  }
};
</script>
<style lang="less" scoped>
.login-main {
  position: relative;
  .back {
    text-align: center;
  }
}
</style>
